<?php


namespace app\admin\controller;

use app\model\hotel\Hotel as HotelModel;
use app\model\shop\Shop as ShopModel;
use app\model\shop\ShopCategory as ShopCategoryModel;
use app\model\shop\ShopGroup as ShopGroupModel;
use app\model\system\Address as AddressModel;
use addon\shopwithdraw\model\Config as ShopWithdrawConfig;

use app\model\member\Member as MemberModel;
use app\model\order\OrderCommon;

use app\model\shop\ShopAccount;
use app\model\shop\ShopApply as ShopApplyModel;

use app\model\shop\ShopDeposit;
use app\model\shop\ShopExport;

use app\model\web\WebSite;
use phpoffice\phpexcel\Classes\PHPExcel;
use phpoffice\phpexcel\Classes\PHPExcel\Writer\Excel2007;
use think\facade\Cache;
use think\facade\Session;
use app\model\web\Config as ConfigModel;

/**
 * 外卖订单管理 控制器
 */
class Localshop extends BaseAdmin
{

    /**
     * 商家列表
     */
    public function lists()
    {
        if (request()->isAjax()) {
            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $search_text = input('search_text', '');
            $category_id = input('category_id', 0);
            $group_id = input('group_id', 0);
            $shop_status = input('shop_status', '');
            $cert_id = input('cert_id', '');
            $is_own = input('is_own', '');
            $start_time = input("start_time", '');
            $end_time = input("end_time", '');
            $website_id = input("website_id", '');
            $code = input('code', '');

            $condition = [
                ['shop_type','=',1]
            ];
            if ($search_text) {
                $condition[] = [ 'site_name|code', 'like', '%' . $search_text . '%' ];
            }
            if ($code) {
                $condition[] = [ 'code', '=', $code ];
            }
            //商家分类
            if ($category_id != 0) {
                $condition[] = [ 'category_id', '=', $category_id ];
            }
            //店铺等级
            if ($group_id != 0) {
                $condition[] = [ 'group_id', '=', $group_id ];
            }
            //商家状态
            if ($shop_status != '') {
                $condition[] = [ 'shop_status', '=', $shop_status ];
            }
            //城市分站
            if ($website_id != '' && $website_id >= 0) {
                $condition[] = [ 'website_id', '=', $website_id ];
            }
            if ($cert_id) {
                switch ( $cert_id ) {
                    case 1: //未认证
                        $condition[] = [ 'cert_id', '=', 0 ];
                        break;
                    case 2: //已认证
                        $condition[] = [ 'cert_id', '>', 0 ];
                        break;
                }
            }
            if ($is_own != '') {
                $condition[] = [ 'is_own', '=', $is_own ];
            }
            if (!empty($start_time) && empty($end_time)) {
                $condition[] = [ 'expire_time', '>=', strtotime($start_time) ];
            } elseif (empty($start_time) && !empty($end_time)) {
                $condition[] = [ "expire_time", "<=", strtotime($end_time) ];
            } elseif (!empty($start_time) && !empty($end_time)) {
                $condition[] = [ "expire_time", ">=", strtotime($start_time) ];
                $condition[] = [ "expire_time", "<=", strtotime($end_time) ];
            }

            $order = 'site_id desc';
            $field = '*';


            $shop_model = new ShopModel();
            // 商家名称site_name 商家分类category_name 店铺等级group_name 是否自营is_own 1：是 0：否 商家状态shop_status 1：正常 0：锁定 有效期
            $shop_list = $shop_model->getShopPageList($condition, $page, $page_size, $order, $field);

            foreach ($shop_list[ 'data' ][ 'list' ] as $k => $v) {
                if ($v[ 'cert_id' ] > 0) {
                    $condition = array (
                        [ 'cert_id', '=', $v[ 'cert_id' ]
                        ] );
                    $cert_info = $shop_model->getShopCert($condition);
                    $shop_list[ 'data' ][ 'list' ][ $k ][ 'cert_info' ] = $cert_info[ 'data' ];
                }
            }
            return $shop_list;

        } else {
            //商家主营行业
            $shop_category_model = new ShopCategoryModel();
            $shop_category_list = $shop_category_model->getCategoryList([], 'category_id, category_name', 'sort asc');
            $this->assign('shop_category_list', $shop_category_list[ 'data' ]);

            //商家主营分组
            $shop_group_model = new ShopGroupModel();
            $shop_group_list = $shop_group_model->getGroupList([ [ 'is_own', '=', 0 ] ], 'group_id,is_own,group_name,fee,remark', 'is_own asc,fee asc');
            $this->assign('shop_group_list', $shop_group_list[ 'data' ]);

            $is_addon_city = addon_is_exit('city');
            $this->assign('is_addon_city', $is_addon_city);


            return $this->fetch('localshop/lists');
        }
    }

    /**
     * 商家添加
     */
    public function addShop()
    {
        if (request()->isAjax()) {

            //店铺信息
            $shop_data = [
                'site_name' => input('site_name', ''),//店铺名称
                'category_id' => input('category_id', 0),//分类id
                'category_name' => input('category_name', ''),//分类名称
                'group_id' => input('group_id', 0),//组id
                'group_name' => input('group_name', ''),//组名称
                'is_own' => input('is_own', 0),//是否自营
                'member_id' => input('member_id', 0),//关联会员 用于前台登陆和相关商家操作
                'year' => input('year', 0),//入驻时长 1 2 3 4 5 下拉选择
                'website_id' => input('website_id', 0),//选择城市分站  下拉选择

                "province" => input('province', ''),
                "province_name" => input('province_name', ''),
                "city" => input('city', ''),
                "city_name" => input('city_name', ''),
                "district" => input('district', ''),
                "district_name" => input('district_name', ''),
                "address" => input('address', ''),
                "full_address" => input('full_address', ''),
                "longitude" => input('longitude', ''),
                "latitude" => input('latitude', ''),
                "start_time" => input('start_time', ''),
                "end_time" => input('end_time', ''),
                "name" => input('name', ''),
                "mobile" => input('mobile', ''),
                "avatar" => input('avatar', ''),
                "banner" => input('banner', ''),
                'contacts_card_electronic_2' => input('contacts_card_electronic_2', ''),//申请人身份证正面
                'contacts_card_electronic_3' => input('contacts_card_electronic_3', ''),//申请人身份证反面
                'contacts_card_no' => input('contacts_card_no', ''),
                'contacts_card_name' => input('contacts_card_name', ''),
                'contacts_card_type' => input('contacts_card_type', ''),
                'contacts_card_start_time' => input('contacts_card_start_time', ''),
                'contacts_card_end_time' => input('contacts_card_end_time', ''),

                'settlement_bank_account_name' => input('settlement_bank_account_name', ''),//开户人
                'settlement_bank_account_number' => input('settlement_bank_account_number', ''),//银行卡号
                'settlement_bank_name' => input('settlement_bank_name', ''),//开户行
                'settlement_bank_type' => input('settlement_bank_type', ''),//开户行
                'settlement_bank_img' => input('settlement_bank_img', ''),//开户行

                'cert_type' => input('cert_type', 1),//申请类型1.个人店铺 2.企业店铺
                'business_licence_number' => input('business_licence_number', ''),//统一社会信用码 input
                'business_licence_number_electronic' => input('business_licence_number_electronic', ''),//营业执照电子版

                'license_time_type' => input('license_time_type', 1),
                'license_start_date' => input('license_start_date', ''),
                'license_end_date' => input('license_end_date', ''),

                'sort' => input('sort', 0),
                'commission_rate' => input('commission_rate', ''),
                'is_recommend' => input('is_recommend', ''),
                'admin_id' => $this->user_info['uid'],
                'admin_username' => $this->user_info['username'],
                'code'=>date('YmdHi') . rand(1111, 9999),
                'per_head' => input('per_head', 0),
                'shop_type'=>1,

                'contact_email' => input('contact_email', ''),
                'legal_addr' => input('legal_addr', ''),
                'legal_cert_validity_type' => input('legal_cert_validity_type', 0),
                'busi_type' => input('busi_type', 1),
            ];

            if (input('shop_label', [])){
                $shop_data['shop_label'] = json_encode(input('shop_label', []),320);
            }
            //dump($shop_data);die;
            //认证信息
            $cert_data = [
                /* 申请类型 */
                'cert_type' => input('cert_type', 1),//申请类型1.个人店铺 2.企业店铺
                /* 公司信息 只有公司类型有 */
                'company_name' => input('company_name', ''),//公司名称
                'company_province_id' => input('company_province_id', ''),//公司所在省
                'company_city_id' => input('company_city_id', ''),//公司所在市
                'company_district_id' => input('company_district_id', ''),//公司所在区/县
                'company_address' => input('company_address', ''),//公司地址
                'company_full_address' => input('company_full_address', ''),//公司完整地址
                /* 联系人手机号身份证 公司、个人类型都有 */
                'contacts_name' => input('contacts_name', ''),//联系人姓名
                'contacts_mobile' => input('contacts_mobile', ''),//联系人手机
                'contacts_card_no' => input('contacts_card_no', ''),//联系人身份证
                'contacts_card_electronic_1' => input('contacts_card_electronic_1', ''),//申请人手持身份证电子版
                'contacts_card_electronic_2' => input('contacts_card_electronic_2', ''),//申请人身份证正面
                'contacts_card_electronic_3' => input('contacts_card_electronic_3', ''),//申请人身份证反面
                /* 营业执照 税务 只有公司类型有 */
                'business_licence_number' => input('business_licence_number', ''),//统一社会信用码 input
                'business_licence_number_electronic' => input('business_licence_number_electronic', ''),//营业执照电子版
                'business_sphere' => input('business_sphere', ''),//法定经营范围 textarea
                'tax_registration_certificate' => input('tax_registration_certificate', ''),//税务登记证号
                'tax_registration_certificate_electronic' => input('tax_registration_certificate_electronic', ''),//税务登记证号电子版
                /* 对公账户信息 只有公司类型有 */
                'bank_account_name' => input('bank_account_name', ''),//银行开户名
                'bank_account_number' => input('bank_account_number', ''),//公司银行账号
                'bank_name' => input('bank_name', ''),//开户银行支行名称
                'bank_address' => input('bank_address', ''),//开户银行所在地 用三级地址选择省市区 传递拼在一起的名字 如山西省太原市小店区
                /* 结算信息 公司、个人类型都有 */
                'bank_type' => input('bank_type', 1),//结算账户类型  1银行卡 2 支付宝
                'settlement_bank_account_name' => input('settlement_bank_account_name', 0),//结算银行开户名
                'settlement_bank_account_number' => input('settlement_bank_account_number', 0),//结算公司银行账号
                'settlement_bank_name' => input('settlement_bank_name', 0),//结算开户银行支行名称
                'settlement_bank_address' => input('settlement_bank_address', 0),//结算开户银行所在地 用三级地址选择省市区 传递拼在一起的名字 如山西省太原市小店区
            ];
            //个人信息
            $user_info = [
                'username' => input('username', ''),
                'password' => data_md5(input('password', '')),
            ];
            $shop_model = new ShopModel();
            $this->addLog("添加商家:" . $shop_data[ 'site_name' ]);
            return $shop_model->addShop($shop_data, $cert_data, $user_info);

        } else {
            //商家主营行业
            $shop_category_model = new ShopCategoryModel();
            $shop_category_list = $shop_category_model->getCategoryList([], 'category_id, category_name', 'sort asc');
            $this->assign('shop_category_list', $shop_category_list[ 'data' ]);

            //商家主营行业
            $shop_group_model = new ShopGroupModel();
            $shop_group_list = $shop_group_model->getGroupList([ ], 'group_id,is_own,group_name,fee,remark', 'is_own asc,fee asc');
            $this->assign('shop_group_list', $shop_group_list[ 'data' ]);
            //商家自营等级
            //$shop_own_group = $shop_group_model->getGroupList([ [ 'is_own', '=', 1 ] ], 'group_id,is_own,group_name,fee,remark', 'fee asc');
            //$this->assign('shop_own_group', $shop_own_group[ 'data' ]);
            //结算账户类型
            $this->assign("support_transfer_type", $this->getTransferType());
            //城市分站


            //查询省级数据列表
            $address_model = new AddressModel();
            $list = $address_model->getAreaList([ [ "pid", "=", 0 ], [ "level", "=", 1 ] ]);
            $this->assign("province_list", $list[ "data" ]);

            //获取地址key配置
            $config_model = new ConfigModel();
            $config = $config_model->getMapConfig();
            $this->assign('map_key', $config[ 'data' ][ 'value' ]);

            $hotel_model = new HotelModel();
            $id_card_type_list = $hotel_model->idCardType();
            $this->assign('id_card_type_list', $id_card_type_list);

            $shop_member_id = model('shop')->getColumn([['member_id','>',0],['shop_type','=',1]],'member_id');

            //dump($shop_member_id);die;
            $member_model = new MemberModel();
            $member_list = $member_model->getMemberList([['member_id','not in',$shop_member_id]],'member_id,nickname,mobile');
            $this->assign('member_list', $member_list['data']);
            return $this->fetch('localshop/add_shop');
        }
    }

    //商家编辑
    public function editShop()
    {
        $shop_model = new ShopModel();

        if (request()->isAjax()){
            $site_id = input('site_id', 0);
            //dump(input('shop_label', []));
            //店铺信息
            $shop_data = [
                'site_name' => input('site_name', ''),//店铺名称
                'category_id' => input('category_id', 0),//分类id
                'category_name' => input('category_name', ''),//分类名称
                'group_id' => input('group_id', 0),//组id
                'group_name' => input('group_name', ''),//组名称
                'is_own' => input('is_own', 0),//是否自营
                //'member_id' => input('member_id', 0),//关联会员 用于前台登陆和相关商家操作
                'year' => input('year', 0),//入驻时长 1 2 3 4 5 下拉选择
                'website_id' => input('website_id', 0),//选择城市分站  下拉选择

                "province" => input('province', ''),
                "province_name" => input('province_name', ''),
                "city" => input('city', ''),
                "city_name" => input('city_name', ''),
                "district" => input('district', ''),
                "district_name" => input('district_name', ''),
                "address" => input('address', ''),
                "full_address" => input('full_address', ''),
                "longitude" => input('longitude', ''),
                "latitude" => input('latitude', ''),
                "start_time" => input('start_time', ''),
                "end_time" => input('end_time', ''),
                "name" => input('name', ''),
                "mobile" => input('mobile', ''),
                "avatar" => input('avatar', ''),
                "banner" => input('banner', ''),
                'contacts_card_electronic_2' => input('contacts_card_electronic_2', ''),//申请人身份证正面
                'contacts_card_electronic_3' => input('contacts_card_electronic_3', ''),//申请人身份证反面
                'contacts_card_no' => input('contacts_card_no', ''),
                'contacts_card_name' => input('contacts_card_name', ''),
                'contacts_card_type' => input('contacts_card_type', ''),
                'contacts_card_start_time' => input('contacts_card_start_time', ''),
                'contacts_card_end_time' => input('contacts_card_end_time', ''),

                'settlement_bank_account_name' => input('settlement_bank_account_name', ''),//开户人
                'settlement_bank_account_number' => input('settlement_bank_account_number', ''),//银行卡号
                'settlement_bank_name' => input('settlement_bank_name', ''),//开户行
                'settlement_bank_type' => input('settlement_bank_type', ''),//开户行
                'settlement_bank_img' => input('settlement_bank_img', ''),//开户行

                //'cert_type' => input('cert_type', 1),//申请类型1.个人店铺 2.企业店铺
                'business_licence_number' => input('business_licence_number', ''),//统一社会信用码 input
                'business_licence_number_electronic' => input('business_licence_number_electronic', ''),//营业执照电子版

                'license_time_type' => input('license_time_type', 1),
                'license_start_date' => input('license_start_date', ''),
                'license_end_date' => input('license_end_date', ''),

                'sort' => input('sort', 0),
                'commission_rate' => input('commission_rate', ''),
                'is_recommend' => input('is_recommend', ''),
                'per_head' => input('per_head', 0),

                'contact_email' => input('contact_email', ''),
                'legal_addr' => input('legal_addr', ''),
                'legal_cert_validity_type' => input('legal_cert_validity_type', 0),
                'busi_type' => input('busi_type', 1),
            ];

            if (input('shop_label', [])){
                $shop_data['shop_label'] = json_encode(array_values(input('shop_label', [])),320);
            } else {
                $shop_data['shop_label'] = '';
            }
            //dump($shop_data['shop_label']);die;
            $shop_model = new ShopModel();

            return $shop_model->editShop($shop_data, [ [ 'site_id', '=', $site_id ] ]);

        } else {
            //商家信息
            $site_id = input('site_id', 0);
            $shop_info = $shop_model->getShopInfo([ [ 'site_id', '=', $site_id ] ]);
            $this->assign('shop_info', $shop_info[ 'data' ]);
            //dump($shop_info[ 'data' ]);die;

            //商家主营行业
            $shop_category_model = new ShopCategoryModel();
            $shop_category_list = $shop_category_model->getCategoryList([], 'category_id, category_name', 'sort asc');
            $this->assign('shop_category_list', $shop_category_list[ 'data' ]);

            //商家开店套餐（非自营）
            $shop_group_model = new ShopGroupModel();
            $shop_group_list = $shop_group_model->getGroupList([  ], 'group_id,is_own,group_name,fee,remark', 'is_own asc,fee asc');
            $this->assign('shop_group_list', $shop_group_list[ 'data' ]);

            $address_model = new AddressModel();
            $list = $address_model->getAreaList([ [ "pid", "=", 0 ], [ "level", "=", 1 ] ]);
            $this->assign("province_list", $list[ "data" ]);

            //获取地址key配置
            $config_model = new ConfigModel();
            $config = $config_model->getMapConfig();
            $this->assign('map_key', $config[ 'data' ][ 'value' ]);

            $hotel_model = new HotelModel();
            $id_card_type_list = $hotel_model->idCardType();
            $this->assign('id_card_type_list', $id_card_type_list);

            $shop_member_id = model('shop')->getColumn([['member_id','>',0]],'member_id');
            //dump($shop_member_id);die;
            $member_model = new MemberModel();
            $member_list = $member_model->getMemberList([],'member_id,nickname,mobile');
            $this->assign('member_list', $member_list['data']);

            return $this->fetch('localshop/edit_shop');
        }

    }

    /**
     * 判断结算类型
     */
    public function getTransferType()
    {

        $support_type = [];
        if (addon_is_exit("shopwithdraw")) {
            $config_model = new ShopWithdrawConfig();
            $config_result = $config_model->getConfig();
            $config = $config_result[ "data" ];
            if ($config[ "is_use" ]) {
                $support_type = explode(",", $config[ 'value' ][ "transfer_type" ]);
            } else {
                $support_type = [ "alipay", "bank" ];
            }
        } else {
            $support_type = [ "alipay", "bank" ];
        }
        return $support_type;
    }



    public function frozen()
    {
        $hotel_model = new ShopModel();

        $site_id = input('site_id', '');
        $is_frozen = input('shop_status', 0);
        $condition = [
            [ "site_id", "=", $site_id ]
        ];

        $res = $hotel_model->frozenShop($condition, $is_frozen);
        return $res;
    }
}